package com.lora.communication.socket;

import com.lora.entity.AlarmList;
import com.lora.enums.EquipmentEnum;
import com.lora.service.AlarmListService;
import com.lora.service.EquipmentService;
import com.lora.util.ApplicationUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ActiveDoor {

    @Async
    public void  activeDoor(){
        EquipmentService equipmentService= ApplicationUtil.getBean(EquipmentService.class);
        Map<String,Object> map=new HashMap<>();
        map.put("type", EquipmentEnum.ACCSS_CONTROL.getValue());
        List<Map<String, Object>> query = equipmentService.query(map);
        if(query!=null&&query.size()>0){
            for (Map<String, Object> stringObjectMap : query) {
                Object position = stringObjectMap.get("parameter");
                if(position!=null&&position.toString().indexOf("-")==1) {
                    String doornum = position.toString().substring(0, 1);
                    Object eid = stringObjectMap.get("eid");
                    Object postion = position.toString().substring(2);
                    try {
                        if (!ping(postion.toString())) {
                            AlarmListService alarmListService = ApplicationUtil.getBean("alarmListService");
                            AlarmList alarmList = new AlarmList();
                            alarmList.setHandleStatus(0);
                            alarmList.setFieldTitle(stringObjectMap.get("position").toString());
                            alarmList.setEpId(eid.toString());
                            alarmList.setAlarmInfo(stringObjectMap.get("position")+"无法链接");
                            alarmList.setFieldName("entranceGuard");
                            alarmListService.save(alarmList);
                        }
                    } catch (Exception e) {
                        AlarmListService alarmListService = ApplicationUtil.getBean("alarmListService");
                        AlarmList alarmList = new AlarmList();
                        alarmList.setHandleStatus(0);
                        alarmList.setFieldTitle(stringObjectMap.get("position").toString());
                        alarmList.setEpId(eid.toString());
                        alarmList.setAlarmInfo(stringObjectMap.get("position")+"无法链接");
                        alarmList.setFieldName("entranceGuard");
                        alarmListService.save(alarmList);
                    }
                }
            }
        }
    }

    @Async
    public void  activeCamera(){
        EquipmentService equipmentService= ApplicationUtil.getBean(EquipmentService.class);
        Map<String,Object> map=new HashMap<>();
        map.put("type", EquipmentEnum.CAMERA.getValue());
        List<Map<String, Object>> query = equipmentService.query(map);
        if(query!=null&&query.size()>0){
            for (Map<String, Object> stringObjectMap : query) {
                Object position = stringObjectMap.get("eid");
                try {
                    if (ping(position.toString())) {
                        AlarmListService alarmListService = ApplicationUtil.getBean("alarmListService");
                        AlarmList alarmList = new AlarmList();
                        alarmList.setHandleStatus(0);
                        alarmList.setFieldTitle(stringObjectMap.get("position").toString());
                        alarmList.setEpId(position.toString());
                        alarmList.setAlarmInfo(stringObjectMap.get("position")+"无法链接");
                        alarmList.setFieldName("camera");
                        alarmListService.save(alarmList);
                    }
                } catch (Exception e) {
                    AlarmListService alarmListService = ApplicationUtil.getBean("alarmListService");
                    AlarmList alarmList = new AlarmList();
                    alarmList.setHandleStatus(0);
                    alarmList.setFieldTitle(stringObjectMap.get("position").toString());
                    alarmList.setEpId(position.toString());
                    alarmList.setAlarmInfo(stringObjectMap.get("position")+"无法链接");
                    alarmList.setFieldName("camera");
                    alarmListService.save(alarmList);
                }
            }
        }
    }

    public static boolean ping(String ipAddress) throws Exception {
        int  timeOut =  3000 ;  //超时应该在3钞以上
        boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);     // 当返回值是true时，说明host是可用的，false则不可。
        return status;
    }
}
